package com.lineage.data.item_etcitem;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_Message_YN;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import java.util.Iterator;

public class Scroll_Resurrection extends ItemExecutor {
  public static ItemExecutor get() {
    return new Scroll_Resurrection();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1Character target = (L1Character)World.get().findObject(targObjId);
    if (target == null)
      return; 
    if (target.getId() == pc.getId())
      return; 
    if (target.getCurrentHp() > 0 && !target.isDead())
      return; 
    pc.getInventory().removeItem(item, 1L);
    if (target.isDead())
      if (target instanceof L1PcInstance) {
        L1PcInstance targetPc = (L1PcInstance)target;
        if (World.get().getVisiblePlayer((L1Object)targetPc, 0).size() > 0) {
          Iterator<L1PcInstance> localIterator = World.get().getVisiblePlayer((L1Object)targetPc, 0).iterator();
          while (localIterator.hasNext()) {
            L1PcInstance visiblePc = localIterator.next();
            if (!visiblePc.isDead()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(592));
              return;
            } 
          } 
        } 
        if (pc.getMap().isUseResurrection()) {
          targetPc.setTempID(pc.getId());
          if (item.getItem().getBless() != 0) {
            targetPc.sendPackets((ServerBasePacket)new S_Message_YN(321));
          } else {
            targetPc.sendPackets((ServerBasePacket)new S_Message_YN(322));
          } 
        } 
      } else if (target instanceof L1NpcInstance && !(target instanceof com.lineage.server.model.Instance.L1TowerInstance)) {
        L1NpcInstance npc = (L1NpcInstance)target;
        if (npc.getNpcTemplate().isCantResurrect())
          return; 
        if (npc instanceof com.lineage.server.model.Instance.L1PetInstance && World.get().getVisiblePlayer((L1Object)npc, 0).size() > 0) {
          Iterator<L1PcInstance> iterator = World.get().getVisiblePlayer((L1Object)npc, 0).iterator();
          while (iterator.hasNext()) {
            L1PcInstance visiblePc = iterator.next();
            if (!visiblePc.isDead()) {
              pc.sendPackets((ServerBasePacket)new S_ServerMessage(592));
              return;
            } 
          } 
        } 
        npc.resurrect(npc.getMaxHp() / 4);
        npc.setResurrect(true);
        npc.setDead(false);
      }  
  }
}
